layui.use(['form','layer'], function(){
    var form=layui.form,layer=layui.layer;
    var id=urlQuery("id");
    /**
     * 显示角色
     */
    $.ajax({
        url:"/admin/role/list",
        type:"get",
        async:false,
        success:function (result) {
            if(result.code==0){
                var agents=result.data;
                for(var i=0;i<agents.length;i++){
                    var opt=$("<option>",{
                        "value":agents[i].id,
                        "text":agents[i].name
                    });
                    $("#role").append(opt);
                }
                form.render("select");
            }

        }
    });




    if(id==null||id==''){
        //添加用户
    }else {
        $("#id").val(id);
        $.ajax({
            url:"/admin/get",
            type:"get",
            data:{id:id},
            success:function (result) {
                if(result.code==0){
                    var user=result.data;
                    $("[name=username]").val(user.username);
                    $("[name=realname]").val(user.realname);
                    $("[name=phone]").val(user.phone);
                    $("[name=roleId]").val(user.roleId);
                    $("[name=id]").val(user.id);
                    form.render("select");
                    /*是否禁用*/
                    var no=$("<input>",{
                        "title":"冻结",
                        "value":"0",
                        "type":"radio",
                        "name":"status"
                    });
                    var yes=$("<input>",{
                        "title":"激活",
                        "value":"1",
                        "type":"radio",
                        "name":"status"
                    });
                    if(user.status){
                        yes.attr("checked",true);
                    }else {
                        no.attr("checked",true);
                    }
                    $("#isStatus").text("");
                    $("#isStatus").append(yes);
                    $("#isStatus").append(no);

                    $(".disabled").attr("disabled",true);
                    $(".disabled").css("background-color","#d8dcdb");
                    $("#gender").css("display","none")
                    form.render("select");
                    form.render();
                }else {
                    layer.msg(result.message);
                }
                form.render();
            }
        });
    }
    //监听提交
    form.on('submit(demo1)', function(data){
        var data=data.field;
        var regPhone = /^1\d{10}$/;
        var roleName=$("#role").find("option:selected").text();
        if(data.username==null||data.username==''){
            layer.msg("请输入登录名", {icon: 2,time:2000,shade:0.2});
            return false;
        }else if(data.realname==null||data.realname==''){
            layer.msg("请输入用户名称", {icon: 2,time:2000,shade:0.2});
            return false;
        }else if(data.phone==null||data.phone==''){
            layer.msg("请输入电话", {icon: 2,time:2000,shade:0.2});
            return false;
        }else if(!regPhone.test(data.phone)){
            layer.msg("请输入正确的电话号码", {icon: 2,time:2000,shade:0.2});
            return false;
        }else if($("#role").val()=="" || $("#role").val()==null){
            layer.msg("请选择角色权限", {icon: 2,time:2000,shade:0.2});
            return false;
        }
        $.ajax({
            url:"/admin/addOrEdit",
            type:"post",
            data:data,
            success:function (result) {
                if(result.code==0){
                    layer.msg(result.message, {icon: 1,time:2000,shade:0.2});
                    setInterval(function () {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index);
                        window.parent.location.reload();
                    },500)
                }else {
                    layer.msg(result.message, {icon: 2,time:2000,shade:0.2});
                }

            }
        });
        return false;
    });

});